1 package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2
3 import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnknownException;
4 import org.apache.commons.io.IOUtils;
5 import org.apache.pdfbox.Loader;
6 import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
7 import org.apache.pdfbox.pdmodel.PDDocument;
8 import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
9
10 import java.io.IOException;
11
12
13
14
15
16
17
18
19
20 public class PdfChecker extends FileChecker {
21
22 public PdfChecker() {
23 super();
24 }
25
26 @Override
27 public String[] getFileExtensions() {
28 return new String[]{"pdf"};
29 }
30
31 @Override
32 public String getFileDescription() {
33 return "*.pdf";
34 }
35
36 @Override
37 public String getDescription() {
38 return "PDF Checker";
39 }
40
41 @Override
42 public boolean prepareChecker() {
43 return true;
44 }
45
46 @Override
47 public boolean checkPassword(String password) {
48 boolean result = false;
49 PDDocument pdDocument = null;
50 try {
51 pdDocument = Loader.loadPDF(new RandomAccessReadBufferedFile(file), password);
52 result = true;
53 } catch (InvalidPasswordException e) {
54 logger.debug("[InvalidPasswordException]password is incorrect: {}", password);
55 } catch (IOException e) {
56 throw new UnknownException(e);
57 } finally {
58 if (pdDocument != null) {
59 IOUtils.closeQuietly(pdDocument);
60 }
61 }
62 return result;
63 }
64 }